// 83. Remove Duplicates from Sorted List

// Given a sorted linked list, delete all duplicates such that each element appear only once.

// For example,
// Given 1->1->2, return 1->2.
// Given 1->1->2->3->3, return 1->2->3.

package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil {
		return head
	}

	refer, current := head, head.Next
	for ; current != nil; current = current.Next {
		if refer.Val != current.Val {
			refer.Next = current
			refer = current
		}
	}
	refer.Next = current

	return head
}

func (l *ListNode) printNode() {
	i := 1
	for l != nil {
		println("i=", i, "v=", l.Val)
		l = l.Next
		i++
	}
}

func main() {
	h, i, j := new(ListNode), 1, 0
	head := h
	for j < 3 {
		h.Val = i
		h.Next = new(ListNode)
		h = h.Next
		j++
		if j%2 == 0 {
			i++
		}
	}
	h.Val = i

	head.printNode()
	head = deleteDuplicates(head)
	println("==========")
	head.printNode()
}
